package parkingSpace

import (
	"fmt"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/parkingSpace"
	parkingSpaceReq "github.com/flipped-aurora/gin-vue-admin/server/model/parkingSpace/request"
)

type ParkingSpaceService struct {
}

// CreateParkingSpace 创建parkingSpace表记录
// Author [piexlmax](https://github.com/piexlmax)
func (parkingSpacesService *ParkingSpaceService) CreateParkingSpace(parkingSpaces *parkingSpace.ParkingSpace) (err error) {
	err = global.GVA_DB.Create(parkingSpaces).Error
	return err
}

// DeleteParkingSpace 删除parkingSpace表记录
// Author [piexlmax](https://github.com/piexlmax)
func (parkingSpacesService *ParkingSpaceService) DeleteParkingSpace(parkingSpaces parkingSpace.ParkingSpace) (err error) {
	err = global.GVA_DB.Delete(&parkingSpaces).Error
	return err
}

// DeleteParkingSpaceByIds 批量删除parkingSpace表记录
// Author [piexlmax](https://github.com/piexlmax)
func (parkingSpacesService *ParkingSpaceService) DeleteParkingSpaceByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]parkingSpace.ParkingSpace{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateParkingSpace 更新parkingSpace表记录
// Author [piexlmax](https://github.com/piexlmax)
func (parkingSpacesService *ParkingSpaceService) UpdateParkingSpace(parkingSpaces parkingSpace.ParkingSpace) (err error) {
	err = global.GVA_DB.Save(&parkingSpaces).Error
	return err
}

// GetParkingSpace 根据id获取parkingSpace表记录
// Author [piexlmax](https://github.com/piexlmax)
func (parkingSpacesService *ParkingSpaceService) GetParkingSpace(id uint) (parkingSpaces parkingSpace.ParkingSpace, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&parkingSpaces).Error
	return
}

// GetParkingSpaceInfoList 分页获取parkingSpace表记录
// Author [piexlmax](https://github.com/piexlmax)
func (parkingSpacesService *ParkingSpaceService) GetParkingSpaceInfoList(info parkingSpaceReq.ParkingSpaceSearch) (list []parkingSpace.ParkingSpace, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&parkingSpace.ParkingSpace{})
	var parkingSpacess []parkingSpace.ParkingSpace
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.ParkingSpaceNum != "" {
		db = db.Where("parking_space_num = ?", info.ParkingSpaceNum)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&parkingSpacess).Error

	return parkingSpacess, total, err
}

func (parkingSpacesService *ParkingSpaceService) GetParkingSpaceInfoListNoPage(garageId int) (list []parkingSpace.ParkingSpace, err error) {
	db := global.GVA_DB.Model(&parkingSpace.ParkingSpace{})
	var parkingSpaces []parkingSpace.ParkingSpace
	err = db.Where("garage_id = ?", garageId).Find(&parkingSpaces).Error
	fmt.Println(parkingSpaces)
	return parkingSpaces, err
}
